<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Rules\Sms;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Str;
class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;
    
    protected $message = [
            'mobile.required' => '手机号必须填写.',
            'mobile.unique' => '手机号已经重复.',
            'code.required' => '验证码必须填写.',
            'password.required' => '密码必须填写.',
            'password.min' => '密码不能小于:min.',
            'code.size' => '验证码长为:size.',
            'code.captcha' => '验证码错误,请点击验证码刷新.',
            'mobile.size' => '手机号长为:size.',
            'mobile.exists' => '不存在用户.'
        ];

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }
    /**
     * Send a reset link to the given user.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
     */
    public function sendResetLinkEmail(Request $request)
    {
        $this->validateMobile($request);
        //save mobile in session
    
        return redirect()->back()->with('status', 2)->with('mobile', $request->mobile);
    }
    
    /**
     * Validate the email for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function validateMobile(Request $request)
    {
        $this->validate($request, ['mobile' => [
                    'required',
                    Rule::exists('users')->where(function ($query) use ($request) {
                        $query->where('mobile', $request->mobile);
                    })
                ]], $this->message);
    }
    
    public function checkCode(Request $request)
    {
        $validator = \Validator::make($request->all(), ['mobile' => [
                    'required',
                    Rule::exists('users')->where(function ($query) use ($request) {
                        $query->where('mobile', $request->mobile);
                    })
                ],'code' => [
                'required',
                new Sms($request->mobile)
            ]], $this->message);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->with('status', 1)->with('mobile', $request->mobile);
        }else {
            //select users info save in session
            $userinfo = User::where('mobile', $request->mobile)->firstOrFail();
            session(['resets_password_userinfo' => $userinfo]);
            return redirect()->back()->with('status', 3)->with('mobile', $request->mobile);
        }
    }
    
    
    public function resetsPassword(Request $request)
    {
        $user = session('resets_password_userinfo');
        
        if(!$user)return redirect()->back();
        
        $validator = \Validator::make($request->all(), ['password' => 'required|min:6'], $this->message);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->with('status', 3)->with('mobile', $request->mobile);
        }else {
            //save password
            $user->password = Hash::make($request->password);

            $user->setRememberToken(Str::random(60));
    
            $user->save();
    
            event(new PasswordReset($user));
    
            //delete the userinfo session
            $request->session()->forget('resets_password_userinfo');
            
            return redirect()->back()->with('status', 4)->with('mobile', $request->mobile);
        }
    }
    
}
